import { getlistApi, getClassifyApi, addlistApi, deleteApi, updateApi } from '@/api/msbd'

export default {
  namespaced: true,
  state: {
    tableData: [],
    sort: 'desc',
    orderby: 'id',
    classify: [],
    search: '',
    currentPage: 1,
    pageSize: 5,
    total: null,
  },
  mutations: {
    SET_LIST(state, data) {
      state.tableData = data

    },
    SET_CLASSIFY(state, classify) {
      state.classify = classify
      // console.log(classify);
    },
    SET_SEARCH(state, data) {
      state.search = data
      // console.log(data);
    },
    SET_CURRENT(state, data) {
      state.currentPage = data
      console.log(data);
    },
    SET_PAGESZ(state, data) {
      state.pageSize = data
      console.log(data);
    },
    SET_TOTAL(state, data) {
      state.total = data[0].total
      // console.log(state.total);
    },
  },
  actions: {
    async getList({ commit, state },) {
      // console.log(data)
      const { data } = await getlistApi({
        sort: state.sort,
        orderby: state.orderby,
        keyword: state.search,
        currentPage: state.currentPage - 1,
        pageSize: state.pageSize
      });
      let list = await getlistApi({
        sort: state.sort,
        orderby: state.orderby,
        keyword: state.search,
        currentPage: state.currentPage - 1,
        pageSize: state.pageSize
      })
      // console.log(list.total)
      // state.total = data.length
      // console.log(total)
      commit("SET_LIST", data)
      commit('SET_TOTAL', list.total)
    },

    async getClassify({ commit }) {
      const { data } = await getClassifyApi()
      // console.log(data);
      commit("SET_CLASSIFY", data)
    },

    async addlist({ dispatch }, form) {
      await addlistApi(form)
      dispatch('getList')
    },
    async delList({ dispatch }, id) {
      console.log(id)
      await deleteApi(id)
      dispatch('getList')
    },
    async updateList({ dispatch }, form) {
      await updateApi(form)
      dispatch('getList')
    }
  },
  getters: {

  },
  moudules: {

  }
}